/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-01-08
 * Time: 18:32
 */
//泛型内容
class MyArray<T> {
    //泛型数组最好这样写，以后常用的！！！
    /*public Object[] obj = new Object[3];
    public T getObj(int i) {
        return (T)obj[i];
    }*/ 
     public T[] obj = (T[])new Object[3];//先这样写，不报错 但是不够好
     public T getObj(int i) {
        return obj[i];
    }
    public void setObj(int i, T val) {
        obj[i] = val;
        System.out.println(obj[i]);
    }
}
public class Demo1 {
    public static  void main(String[] args) {
        MyArray<Integer> myArray1 = new MyArray<Integer>();
        //存储指定类型
        myArray1.setObj(0,10);
        MyArray<String> myArray2 = new MyArray<>(); //后面的<>里面可以不写 前面的必须写(不然回到之前Object问题)
        myArray2.setObj(1,"come");
    }
}


/*拓展内容,如何要一个具体类型的数组
class MyArray<T> {
    public T[] obj = (T[])new Object[3];
    public MyArray(Class<T> clazz,int capacity) {
        //反射
        obj = (T[])Array.newInstance(clazz,capacity);
    }
    public T getObj(int i) {
        return obj[i];
    }
    public void setObj(int i, T val) {
        obj[i] = val;
    }
    public T[] getArray() {
        return obj;
    }
}
public class Demo1 {
    public static void main(String[] args) {
        MyArray<Integer> myArray = new MyArray<>(Integer.class,10);
        myArray.setObj(0,10);
        myArray.setObj(1,20);
        Integer[] integers = (Integer[])myArray.getArray();
    }
}*/